home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / linuxcon.000 / linuxcon / linuxconf-1.6 / askrunlevel / boot.c < prev    next >
C/C++ Source or Header  |  1996-03-02  |  3KB  |  140 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <fcntl.h>
  6. #include <sys/types.h>
  7. #include <sys/time.h>
  8. #include "askrunlevel.h"
  9. #include "../misc/misc.h"
  10. #include "../paths.h"
  11. #include "askrunlevel.m"
  12.  
  13. static HELP_FILE help_boot_log (HELP_ASKRUN,"bootlog");
  14.  
  15. static CONFIG_FILE f_boot_log (VAR_ADM_BOOT_LOG
  16.     ,help_boot_log
  17.     ,CONFIGF_OPTIONNAL|CONFIGF_MANAGED);
  18.  
  19. static const char key[]="#########";
  20. static const int sizekey = 9;
  21. /*
  22.     Store the boot message in /var/adm/boot.log
  23. */
  24. void boot_save2log()
  25. {
  26.     /* #Specification: askrunlevel / boot log
  27.         At each boot, askrunlevel grab the log (/proc/kmsg) and
  28.         stores it in /var/adm/boot.log. askrunlevel can present
  29.         these logs to the user later.
  30.  
  31.         For this reason, you won't find this information in
  32.         /var/adm/messages like a normal linux.
  33.     */
  34.     FILE *fout = f_boot_log.fopen ("a");
  35.     if (fout != NULL){
  36.         {
  37.             time_t tim = time(NULL);
  38.             const char *datestr = asctime(localtime(&tim));
  39.             fprintf (fout,"%s %s\n",key,datestr);
  40.         }
  41.         int fin = open ("/proc/kmsg",O_RDONLY|O_NDELAY);
  42.         if (fin != -1){
  43.             fd_set set;
  44.             FD_ZERO (&set);
  45.             FD_SET (fin,&set);
  46.             struct timeval timeout;
  47.             timeout.tv_usec = timeout.tv_sec = 0;
  48.             if (select(fin+1,&set,NULL,NULL,&timeout)>0){
  49.                 char buf[5000];
  50.                 int nb = read (fin,buf,sizeof(buf)-1);
  51.                 if (nb >= 0){
  52.                     buf[nb] = '\0';
  53.                     if (fout != NULL){
  54.                         fputs (buf,fout);
  55.                     }
  56.                 }
  57.             }
  58.             close (fin);
  59.         }
  60.         fclose (fout);
  61.     }    
  62. }
  63.  
  64. /*
  65.     Locate all boot intro or a specific log in the log file
  66.  
  67.     if choice == -1, get all intro
  68.     if choice != -1. get one log
  69. */
  70. static int boot_getlist(SSTRINGS &lst, int choice)
  71. {
  72.     FILE *fin = f_boot_log.fopen ("r");
  73.     if (fin != NULL){
  74.         char buf[300];
  75.         int collect = 0;
  76.         int nbboot = 0;
  77.         while (fgets(buf,sizeof(buf)-1,fin)!=NULL){
  78.             strip_end (buf);
  79.             if (strncmp(buf,key,sizekey)==0){
  80.                 if (choice == -1){
  81.                     lst.add (new SSTRING (buf+sizekey+1));
  82.                 }else if (choice == nbboot){
  83.                     collect = 1;
  84.                 }else{
  85.                     collect = 0;
  86.                 }
  87.                 nbboot++;
  88.             }else if (collect){
  89.                 lst.add (new SSTRING (buf));
  90.             }
  91.         }
  92.         fclose (fin);
  93.     }
  94.     return lst.getnb();
  95. }
  96.  
  97.  
  98. /*
  99.     Present the logs of the last boots
  100. */
  101. void boot_showlog ()
  102. {
  103.     SSTRINGS lst;
  104.     int nb = boot_getlist(lst,-1);
  105.     if (nb == 0){
  106.         xconf_error (MSG_U(E_NOLOG,"No log available"));
  107.     }else{
  108.         int choice = nb - 1;
  109.         while (1){
  110.             const char **menul = (const char **)malloc((lst.getnb()*2+1)
  111.                 * sizeof(char*));
  112.             int ii = 0;
  113.             for (int i=0; i<lst.getnb(); i++){
  114.                 SSTRING *st = lst.getitem(i);
  115.                 menul[ii++] = " ";
  116.                 menul[ii++] = st->get();
  117.             }
  118.             menul[ii] = NULL;
  119.             MENU_STATUS code = xconf_menu (MSG_U(T_BOOTLOG,"boot log")
  120.                 ,MSG_U(I_BOOTLOG
  121.                  ,"These are the recording of the previous\n"
  122.                   "boot of this machine")
  123.                 ,help_boot_log
  124.                 ,menul
  125.                 ,choice);
  126.             if (code == MENU_OK){
  127.                 SSTRINGS onelog;
  128.                 if (boot_getlist(onelog,choice)==0){
  129.                     xconf_error (MSG_U(E_EMPTYLOG,"Empty log"));
  130.                 }else{
  131.                     dialog_textbox (lst.getitem(choice)->get(),onelog);
  132.                 }
  133.             }else{
  134.                 break;
  135.             }
  136.         }
  137.     }
  138. }
  139.  
  140.